package net.tngou.crawler.drug;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.math.NumberUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import net.tngou.c4j.util.HttpUtil;
import net.tngou.cache.CacheEngine;
import net.tngou.cache.TgCacheEngine;
import net.tngou.crawler.util.StringUtil;

/**
 * 康爱多
 * @author tngou@tngou.net
 *
 */
public class Com_360kad extends BaseCrawler implements CrawlerDrug{
	private static final Logger log= LoggerFactory.getLogger(Com_360kad.class);
	private String url="http://www.360kad.com/product/${}.shtml";
	private String purl="http://www.360kad.com/product/getprice";
	


	@Override
	public JSONObject getJson(URL url) {
		log.info("请求页面:{}",url.toString());
		Document doc = HttpUtil.get(url.toString());
		if(doc==null) return null;
		json.put("url", url.toString());
		json.put("onlinepharmacy", onlinepharmacy);
		//唯一值
		String[] uns = url.toString().split("/");
		json.put("un",uns[uns.length-1].replaceAll(".shtml", "-").split("-")[0]);
		//标签
		Elements detailnav = doc.getElementsByClass("wrap-dtl-nav");
		if(detailnav.first()!=null)
		{
			Elements as = detailnav.first().getElementsByTag("a");
			List<String> list = new ArrayList<>();
			for (int i = 1; i < as.size()-2; i++) {
				list.add(as.get(i).text());
			}				
			json.put("tag", list);
		}
		//图片
		Elements minPicScrolldiv = doc.getElementsByClass("minPicScrolldiv");
		if(minPicScrolldiv.first()!=null)
		{
			Elements imgs = minPicScrolldiv.first().getElementsByTag("img");
			List<String> list = new ArrayList<>();
			imgs.forEach(e->{
				String src = e.attr("src");
				if(StringUtil.isNotEmpty(src))
				{
					list.add(src);
				}
			});
			json.put("picture", list);
		}
		//价格
		json.put("price", getPrice(json.getString("un")));
		//类型
		Elements proname = doc.getElementsByClass("prem-proname");
		if(proname.first()!=null) {
			String s = proname.first().html().toString();
			if(StringUtil.isNotEmpty(s))
			{
				if(s.toUpperCase().indexOf("RX")>=0)
				{
					json.put("ptype", Ptype.RX处方药.ordinal());
				}if(s.toUpperCase().indexOf("OTC")>=0)
				{
					if(s.toUpperCase().indexOf("AOTC")>=0)
					{
						json.put("ptype", Ptype.OTC非处方甲类.ordinal());
					
					}else
					{
						json.put("ptype", Ptype.OTC非处方乙类.ordinal());
					}
				
				}
			}
		
		}
		//说明书
		Elements instruction = doc.getElementsByClass("instructions-ul");
		if(instruction.first()!=null)
		{
			JSONObject instructions = instructions(instruction.first());
		
			json.put("instructions",instructions );
			
		}
		return json;
	}
	
	private JSONObject instructions(Element element) {
		JSONObject json = new JSONObject(true);
		String text = element.getElementsByTag("p").text();
		if(StringUtil.isNotEmpty(text))
		{
			String[] ts = StringUtil.split(text, "【");
			for (String e : ts) {
				String[] es = StringUtil.split(e, "】");
				if(es.length==2) {
					String key=StringUtil.cleanKey(es[0]);
					String value=StringUtil.RemoveEmpty(es[1]);
					if("药品名称".equals(es[0]))
					{
						String t =replaceAll(es[1]);
						String[] ys = StringUtil.split(t, "|");
						for (String s : ys) {
							String[] ss = StringUtil.split(s, "：");
							if(ss.length==2)
							{
								json.put(StringUtil.cleanKey(ss[0]), StringUtil.RemoveEmpty(ss[1]));
							}
						}				
					}else
					{
					
						
						json.put(key,value);
						
						
					}
					
				}
			}
			
		}
		
		return name_number(json);
	}
	public float getPrice(String itemid) {
		String rs = HttpUtil.getString(purl, new HashMap<String,String>() {{put("wareskucode", itemid);}});
		if(rs!=null)
		{	
			JSONObject rjs = (JSONObject) JSON.parse(rs);
			if(rjs!=null) {
				JSONObject item=rjs.getJSONObject("StyleInfo");
				if(item!=null)
					return item.getFloatValue("Price");
			}		
		}
		return 0;
	}
	
}
